package com.study.talk.ui.app.self

import android.os.Bundle
import android.view.Menu
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.study.talk.R
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.ui.app.moment.MomentsActivity
import com.study.talk.data.model.UserInfo
import com.study.talk.databinding.ActivitySelfBinding
import com.study.talk.utils.extras.toMoments
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SelfActivity : BaseActivity() {

    companion object {
        private const val TAG = "SelfActivity"
    }

    private val binding: ActivitySelfBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_self)
    }

    private val vm: SelfViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.userInfoSelfToolbar)
        binding.vm = vm
        binding.lifecycleOwner = this
        initView(app.userInfo)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.moment, menu)
        return super.onCreateOptionsMenu(menu)
    }

    private fun initView(user: UserInfo) {
        binding.userInfoSelfToolbar.setNavigationOnClickListener { finish() }
        binding.userInfoSelfToolbar.setOnMenuItemClickListener { item ->
            if (item.itemId == R.id.user_info_moment) {
                toMoments(MomentsActivity.MOMENT_SELF,user.id)
            }
            true
        }
        binding.userInfoListView.adapter = ArrayAdapter(
            this@SelfActivity, android.R.layout.simple_list_item_1, createListViewData(user)
        )
    }


    private fun createListViewData(user: UserInfo): List<String> {
        val listViewData = mutableListOf<String>()
        listViewData.add("性别：" + (if (user.gender == 1) "男" else "女"))
        listViewData.add("生日：" + user.birth)
        listViewData.add("电话：" + user.phone)
        listViewData.add("邮箱：" + user.email)
        listViewData.add("地址：" + user.address)
        listViewData.add("简介：" + user.introduce)
        return listViewData
    }
}